package util
{
	import embeds.EmbedRes;
	
	import starling.textures.Texture;
	import starling.textures.TextureAtlas;
	
	/**
	 * 图集纹理生成器
	 * @author £iang
	 * 
	 */	
	public class AtlasTextureGenerator
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		//-----------------------------
		// properties
		//-----------------------------
		
		/** 艾露猫图集 */
		private static var _catAtlas:TextureAtlas;
		/** 怪物图集 */
		private static var _monsterAtlas:TextureAtlas;
		/** 道具图集 */
		private static var _itemAtlas:TextureAtlas;
		/** 异常属性图集 */
		private static var _attributeAtlas:TextureAtlas;
		/** 武器图标图集 */
		private static var _weaponAtlas:TextureAtlas;
		/** 猎虫图标图集 */
		private static var _bugAtlas:TextureAtlas;
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化图集 
		 * 
		 */		
		public static function initialize():void
		{
			_catAtlas = new TextureAtlas( 
				Texture.fromBitmapData( new EmbedRes.ATLAS_IMAGE_CAT().bitmapData ),
				XML( new EmbedRes.ATLAS_XML_CAT() )
			);
			
			_monsterAtlas = new TextureAtlas(
				Texture.fromBitmapData( new EmbedRes.ATLAS_IMAGE_MONSTER().bitmapData ),
				XML( new EmbedRes.ATLAS_XML_MONSTER() )
			);
			
			_itemAtlas = new TextureAtlas(
				Texture.fromBitmapData( new EmbedRes.ATLAS_IMAGE_ITEM().bitmapData ),
				XML( new EmbedRes.ATLAS_XML_ITEM() )
			);
			
			_attributeAtlas = new TextureAtlas(
				Texture.fromBitmapData( new EmbedRes.ATLAS_IMAGE_ATTRIBUTE().bitmapData ),
				XML( new EmbedRes.ATLAS_XML_ATTRIBUTE() )
			);
			
			_weaponAtlas = new TextureAtlas(
				Texture.fromBitmapData( new EmbedRes.ATLAS_IMAGE_WEAPON().bitmapData ),
				XML( new EmbedRes.ATLAS_XML_WEAPON() )
			);
			
			_bugAtlas = new TextureAtlas(
				Texture.fromBitmapData( new EmbedRes.ATLAS_IMAGE_BUG().bitmapData ),
				XML( new EmbedRes.ATLAS_XML_BUG() )
			);
		}
		
		/**
		 * 获取猫图集中的某张贴图 
		 * @param name 贴图名
		 * @return 贴图
		 * 
		 */		
		public static function getCatTextureByName( name:String ):Texture
		{
			var fullName:String = "cat_" + name;
			
			return _catAtlas.getTexture( fullName );
		}
		
		/**
		 * 获取随机的艾露猫贴图数组 
		 * @param part 全身或半身 “all or head”
		 * @param total 需要数量
		 * @param needLongPic 是否使用超长的图标
		 * @return  贴图数组
		 * 
		 */		
		public static function getRandomCatTextures( part:String, total:int, needLongPic:Boolean = true ):Vector.<Texture>
		{
			var result:Vector.<Texture> = new Vector.<Texture>;
            var leftCount:int = total;
			var indexArr:Array = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ];
			var i:int;
			var len:int;
			var indexLen:int;
			
			if ( !needLongPic )
				indexArr = [ 1, 3, 4, 5, 6, 8, 9 ];
			
			indexLen = indexArr.length;
			
			if ( total > indexLen )
			{
				var addedCount:int = total - indexLen;
				var addedIter:int = -1;
				
				for ( i = 0; i < addedCount; i ++ )
				{
					addedIter++;
					
					if ( addedIter == indexLen )
						addedIter = 0;
					
					indexArr.push( indexArr[ addedIter ] );
				}
			}
			
			while( leftCount > 0 )
			{
				len = indexArr.length;
				
				var randomNum:int = int( Math.random() * len );
				
				for ( i = 0; i < len; i ++ )
				{
					if ( i == randomNum )
					{
						result.push( _catAtlas.getTexture( "cat_" + part + indexArr[ i ] ) );
						indexArr.splice( i, 1 );
						leftCount--;
						break;
					}
				}
			}
			
			return result;
		}
		
		/**
		 * 通过怪物id获取怪物图标 
		 * @param type 大型或小型 “l” and “s”
		 * @param monsterId 怪物id
		 * @return 贴图
		 * 
		 */	
		public static function getMonsterTextureById( type:String, monsterId:int ):Texture
		{
			var fullName:String = type + "monster_" + monsterId;
			
			return _monsterAtlas.getTexture( fullName );
		}
		
		/**
		 * 获取道具集中的某张贴图 
		 * @param iconId 贴图id
		 * @return 贴图
		 * 
		 */		
		public static function getItemTextureById( iconId:int ):Texture
		{
			var fullName:String = "type" + iconId;
			
			return _itemAtlas.getTexture( fullName );
		}
		
		/**
		 * 通过属性id获取属性图标 
		 * @param aId 属性id
		 * @return 贴图
		 * 
		 */	
		public static function getAttributeTextureById( aId:int ):Texture
		{
			var fullName:String = "attribute" + aId;
			
			return _attributeAtlas.getTexture( fullName );
		}
		
		/**
		 * 获取武器图标
		 * @param name 武器名
		 * @param wId 图标id
		 * @return 贴图
		 * 
		 */	
		public static function getWeaponIconTextureById( name:String, wId:int ):Texture
		{
			var fullName:String = name + wId;
			
			return _weaponAtlas.getTexture( fullName );
		}
		
		/**
		 * 获取猎虫图标
		 * @param bId 图标id
		 * @return 贴图
		 * 
		 */	
		public static function getBugIconTextureById( bId:int ):Texture
		{
			var fullName:String = "bug" + bId;
			
			return _bugAtlas.getTexture( fullName );
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function AtlasTextureGenerator()
		{
		}
	}
}